package stz.flash.main;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import stz.flash.main.flashlight.field.BacklightField;
import stz.flash.main.flashlight.field.FlashlightField;
import stz.flash.main.flashlight.field.LedField;
import stz.flash.main.flashlight.model.BacklightFieldChangeListener;
import stz.flash.main.strobe.field.MonkeyField;
import stz.flash.main.strobe.field.StrobeControlBar;


public class MainManager extends Manager { 
	
	public static final int WIDTH = Display.getWidth() * 2;
	
	//FlashlightMode 관련 
	private static LedField ledField = new LedField();
	private static BacklightField backlightField = new BacklightField();
	private static FlashlightField flashlightField = new FlashlightField();

	//StrobeMode 관련
	private MonkeyField monkeyField = new MonkeyField();
	private StrobeControlBar controlBar = StrobeControlBar.getIns();
	
	private static MainManager instance = new MainManager();
	
	Field testField = new Field() {
		
		protected void paint(Graphics graphics) {
		}
		
		protected void layout(int width, int height) {
			setExtent(getPreferredWidth(), getPreferredHeight());
		}
		
		public int getPreferredWidth() {
			return WIDTH;
		};
		public int getPreferredHeight() {
			return Display.getHeight();
		};
	};
	
	private MainManager() {
		super(NON_FOCUSABLE);

		new BacklightFieldChangeListener(this).update(null, null);
		add(testField);
		
		// Flashlgiht 관련
		add(ledField);
		add(backlightField);
		add(flashlightField);
		
		// Strobe 관련
		add(monkeyField);
		add(controlBar);
	}
	
	public Field getTestField() {
		return testField;
	}

	public static MainManager getIns() {
		return instance;
	}

	protected void sublayout(int width, int height) {
		
		setExtent(WIDTH , Display.getHeight());

		/*
		 * Flashlight Mode 관련
		 */
		// text이미지들
		setPositionChild(ledField, 
				ImageXY.LED[0], 
				ImageXY.LED[1]);
		layoutChild(ledField, 
				ledField.getPreferredWidth(),
				ledField.getPreferredHeight());
		
		setPositionChild(backlightField, 
				ImageXY.BACKLIGHT[0], 
				ImageXY.BACKLIGHT[1]);
		layoutChild(backlightField, 
				backlightField.getPreferredWidth(), 
				backlightField.getPreferredHeight());
		
		setPositionChild(flashlightField, 
				ImageXY.FLASHLIGHT[0], 
				ImageXY.FLASHLIGHT[1]);
		layoutChild(flashlightField, 
				flashlightField.getPreferredWidth(), 
				flashlightField.getPreferredHeight());
		
		
		/*
		 * Strobe Mode 관련
		 */
		setPositionChild(monkeyField, 
				Display.getWidth(), 
				0);
		layoutChild(monkeyField, 
				monkeyField.getPreferredWidth(), 
				monkeyField.getPreferredHeight());
		
		setPositionChild(controlBar, 
				ImageXY.CONTROL_BAR[0], 
				ImageXY.CONTROL_BAR[1]);
		layoutChild(controlBar, 
				controlBar.getPreferredWidth(), 
				controlBar.getPreferredHeight());
		
		//test
		setPositionChild(testField, 0, 0);
		layoutChild(testField, 
				testField.getPreferredWidth(), 
				testField.getPreferredHeight());;
	}
	
	
	public int getPreferredWidth() {
		return WIDTH;
	}
	
	public int getPreferredHeight() {
		return Display.getHeight();
	}
}

